import java.util.*;

/**
 * 2034. 股票价格波动
 */
public class No2034 {
    private final TreeMap<Integer, Integer> database;
    private final TreeMap<Integer, List<Integer>> priceTime;

    public No2034() {
        this.priceTime = new TreeMap<>();
        this.database = new TreeMap<>();
    }

    public void update(int timestamp, int price) {
        if (database.containsKey(timestamp)) {
            Integer ptKey = database.get(timestamp);
            List<Integer> list = priceTime.get(ptKey);
            if (list.size() == 1) priceTime.remove(ptKey);
            else {
                list.remove((Object) timestamp);
                priceTime.put(ptKey, list);
            }
        }
        database.put(timestamp, price);
        List<Integer> list = priceTime.containsKey(price) ? priceTime.get(price) : new LinkedList<>();
        list.add(timestamp);
        priceTime.put(price, list);
    }

    public int current() {
        return database.lastEntry().getValue();
    }

    public int maximum() {
        return priceTime.firstEntry().getKey();
    }

    public int minimum() {
        return priceTime.lastEntry().getKey();
    }
}
